﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace Maydew.Windows.Forms.Controls.FlexiCell
{
    /// <summary>
    /// 单元格边框样式信息。
    /// </summary>
    public class CellBorderStyle
    {
        #region " 事件 "
        /// <summary>
        /// 当属性值改变后发生。
        /// </summary>
        public event EventHandler PropertyChanged;
        #endregion

        #region " 私有变量 "
        /// <summary>
        /// 获取或设置线条样式。
        /// </summary>
        private DashStyle _dashStyle;
        /// <summary>
        /// 获取或设置线条宽度。
        /// </summary>
        private float _width;
        /// <summary>
        /// 获取或设置线条颜色(当值为 Color.Transparent 时，使用默认颜色)。
        /// </summary>
        private Color _color;
        /// <summary>
        /// 获取或设置笔刷。
        /// </summary>
        private Brush _brush;
        /// <summary>
        /// 获取是否具有边框。
        /// </summary>
        private bool _hasBorder;
        #endregion

        #region " 构造函数 "
        /// <summary>
        /// 初始化 CEllBorderStyle 类的新实例。
        /// </summary>
        public CellBorderStyle()
        {
            _dashStyle = DashStyle.Solid;
            _width = 1;
            _color = Color.Transparent;
        }
        #endregion

        #region " 属性 "
        /// <summary>
        /// 获取或设置线条样式。
        /// </summary>
        public DashStyle DashStyle
        {
            get { return _dashStyle; }
            set
            {
                if (!_dashStyle.Equals(value))
                {
                    _dashStyle = value;
                    OnPropertyChanged(EventArgs.Empty);
                }
            }
        }
        /// <summary>
        /// 获取或设置线条宽度。
        /// </summary>
        public float Width
        {
            get { return _width; }
            set
            {
                if (_width != value)
                {
                    _width = value;
                    OnPropertyChanged(EventArgs.Empty);
                }
            }
        }
        /// <summary>
        /// 获取或设置线条颜色(当值为 Color.Transparent 时，使用默认颜色)。
        /// </summary>
        public Color Color
        {
            get { return _color; }
            set
            {
                if (!_color.Equals(value))
                {
                    _color = value;
                    _hasBorder = !Color.Transparent.Equals(_color);
                    OnPropertyChanged(EventArgs.Empty);
                }
            }
        }
        /// <summary>
        /// 获取或设置笔刷。
        /// </summary>
        public Brush Brush
        {
            get { return _brush; }
            set 
            {
                if (_brush==null || !_brush.Equals(value))
                {
                    _brush = value;
                    OnPropertyChanged(EventArgs.Empty);
                }
            }
        }
        /// <summary>
        /// 获取是否具有边框。
        /// </summary>
        internal protected bool HasBorder
        {
            get { return _hasBorder; }
        }
        #endregion

        #region " 方法 "
        /// <summary>
        /// 获取 Pen 对象。
        /// </summary>
        /// <param name="defaultColor">默认颜色。</param>
        /// <returns>返回 Pen 对象。</returns>
        public Pen GetPen(Color defaultColor)
        {
            Pen pen = null;
            if (_dashStyle == DashStyle.Custom)
            {
                pen = new Pen(_brush, _width);
            }
            else
            {
                Color color = _color;
                float width = _width;
                DashStyle dashStyle = _dashStyle;
                if (!HasBorder)
                {
                    color = defaultColor;
                    width = 1;
                    dashStyle = DashStyle.Solid;
                }
                pen = new Pen(color, width);
                pen.DashStyle = dashStyle;
            }
            return pen;
        }
        #endregion

        #region " 友元方法--事件触发 "
        /// <summary>
        /// 触发 PropertyChanged 事件。
        /// </summary>
        /// <param name="e">事件参数。</param>
        protected virtual void OnPropertyChanged(EventArgs e)
        {
            if (PropertyChanged != null) PropertyChanged(this, e);
        }
        #endregion
    }
}
